package de.tabuma.pm.gadget.view;

import de.tabuma.lib.tabumaview;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import de.tabuma.pm.*;
import de.tabuma.pm.gadget.entity.*;

/**
 *
 * @author alefen
 */
public class list extends tabumaview {
    public list(String servlet, Hashtable hashtable) {
        super(servlet, hashtable);
    }
    
    @Override
    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        String noEntry = "no entry";
        List<Gadget> gList = (List<Gadget>) request.getAttribute(constants.gadgetList);
        String name = "name";
        String remove = "remove";
        String settings = "settings";
        String addGadget = "add gadget";

        out.println("<table class=\"contenttable\">");
        out.println("<tr>");
        out.println("<th class=\"contentth\">" + name + "</th>");
        out.println("<th class=\"contentth\">" + settings + "</th>");
        out.println("<th class=\"contentth\">" + remove + "</th>");
        out.println("</tr>");
        String link = this.getLinkAsAttribute();
        for(int i=0;i<gList.size();i++) {
            Gadget gadget = gList.get(i);
            String linkUpdate   = this.getServlet() + "?" + link + "&amp;" + constants.gadgetActionId + "=" + constants.GADGET_ACTION_UPDATE + "&amp;" + constants.gadgetId + "=" + gadget.getId().intValue() + "#" + constants.gadgetA;
            String linkRemove   = this.getServlet() + "?" + link + "&amp;" + constants.gadgetActionId + "=" + constants.GADGET_ACTION_REMOVE + "&amp;" + constants.gadgetId + "=" + gadget.getId().intValue() + "#" + constants.gadgetA;
            String linkSettings = this.getServlet() + "?" + link + "&amp;" + constants.gadgetActionId + "=" + constants.GADGET_ACTION_SETTING + "&amp;" + constants.gadgetId + "=" + gadget.getId().intValue() + "#" + constants.gadgetA;
            out.println("<tr>");
            out.println("<td class=\"contenttd\"><a href=\""+linkUpdate+"\">"+gadget.getName()+"</a></td>");
            out.println("<td class=\"contenttd\"><a href=\""+linkSettings+"\">"+settings+"</a></td>");
            out.println("<td class=\"contenttd\"><a href=\""+linkRemove+"\">"+remove+"</a></td>");
            out.println("</tr>");
        }
        if(gList.size()==0) {
            out.println("<tr>");
            out.println("<td class=\"contenttd\" colspan=\"3\">" + noEntry + "</td>");
            out.println("</tr>");
        }
        out.println("</table>");
        out.println(this.getFormBeginn(constants.gadgetA));
        out.println(this.getHiddenAsAttribute());
        out.println(printHiddenField(constants.gadgetActionId, "" + constants.GADGET_ACTION_CREATE));
        out.println("<input type=\"submit\" class=\"button\" value=\"" + addGadget + "\"/>");
        out.println(this.getFormEnd());
    }
}